package com.erp.module.zhichengJianyan

import android.content.Context
import android.util.Log
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.ProcessInspectionJieHaoBean
import com.erp.contacts.Contacts
import com.erp.util.Httputil
import com.erp.util.Httputil.Call
import com.erp.util.Myutil
import org.xutils.http.HttpMethod
import org.xutils.http.RequestParams
import org.xutils.x
import java.io.File

class ProcessInspectionModel {

    /**获取分配节号*/
    fun getInspection(context: Context, page: Int, pagesize: Int, souSuoBean: InspectionSouSuobean, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.PROCESS_INSPECTION)
        Log.d("TAAAG",souSuoBean.toString())
        Httputil.getparams(context, params)
        Httputil.setProsess(params)
        params.addBodyParameter("page", page.toString())
        params.addBodyParameter("pagesize",pagesize.toString())
        params.addBodyParameter("searchParams[inspection]",souSuoBean.inspection)
        params.addBodyParameter("searchParams[m_codes]",souSuoBean.code)
        params.addBodyParameter("searchParams[mb_area_num]", souSuoBean.quhao)
        params.addBodyParameter("searchParams[mc_id][]",souSuoBean.wuliao)
        if(souSuoBean.time_category != ""){
            params.addBodyParameter("searchParams[time_filter][time_category]", souSuoBean.time_category)
            params.addBodyParameter("searchParams[time_filter][start_time]", Myutil.dateToStamp(souSuoBean.start_time))
            params.addBodyParameter("searchParams[time_filter][end_time]", Myutil.dateToStamp(souSuoBean.end_time))
        }
        params.addHeader("ACCEPT", "application/json");
        x.http().get(params, Httputil.Call(context, onUserDataInterface))
    }

    fun postInspection(context: Context,batch : String,is_inspection : String,onUserDataInterface: OnUserDataInterface){
        val params = RequestParams(Contacts.PROCESS_INSPECTION+"/inspection")
        Httputil.getparams(context, params)
        Httputil.addtoken(params)
        Httputil.setProsess(params)
        params.addBodyParameter("batch", batch, Contacts.BOUNDARY)
        params.addBodyParameter("is_inspection", is_inspection, Contacts.BOUNDARY)
        x.http().post(params, Httputil.Call(context, onUserDataInterface))
    }

    fun addInspection(context: Context, bean: ProcessInspectionJieHaoBean, deletephoto: List<ProcessInspectionJieHaoBean.photo>, onUserDataInterface: OnUserDataInterface){
        var params: RequestParams? = null
        if(bean.inspection_id == ""){
            params = RequestParams(Contacts.PROCESS_INSPECTION)
        }else{
            params = RequestParams(Contacts.PROCESS_INSPECTION + "/" + bean.inspection_id + "/edit")
        }
        Httputil.getparams(context, params)
        Httputil.addtoken(params)
        Httputil.setProsess(params)
        params!!.addHeader("ACCEPT", "application/json")
        params.addBodyParameter("", "", Contacts.BOUNDARY)
        params.addBodyParameter("batch",bean.batch)
        for (i in bean.listphoto.indices) {
            val photo = bean.listphoto[i]
            if (photo.fid == "") {
                params.addBodyParameter("files[new][$i]", File(photo.uri), Contacts.BOUNDARY)
            }
        }
        for (i in deletephoto.indices) {
            if (deletephoto[i].fid != "") {
                params.addBodyParameter("files[delete][" + deletephoto[i].fid + "]", deletephoto[i].fid, Contacts.BOUNDARY)
            }
        }
        x.http().post<String>(params, Call(context, onUserDataInterface))
    }

    fun getInspection(context: Context, id: String, onUserDataInterface: OnUserDataInterface){
        var params: RequestParams? = null
        params = RequestParams(Contacts.PROCESS_INSPECTION+"/"+id)
        Httputil.getparams(context, params)
        Httputil.setProsess(params)
        x.http().get<String>(params, Call(context, onUserDataInterface))
    }

    fun deleteInspection(context: Context, id: String, onUserDataInterface: OnUserDataInterface){
        var params = RequestParams(Contacts.PROCESS_INSPECTION+"/"+id)
        Httputil.getparams(context, params)
        Httputil.addtoken(params)
        x.http().request<String>(HttpMethod.DELETE, params, Call(context, onUserDataInterface))
    }

    /**获取物料类别代码 */
    fun getWuLiao_LeiBie_DaiMa(context: Context, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.WULIAO_DAIMA)
        Httputil.getparams(context, params)
        x.http().get<String>(params, Call(context, onUserDataInterface))
    }
}